#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.IO;
using System.Text;

using NUnit.Framework;

using octalforty.Componento.ComponentModel.Installation;
using octalforty.Componento.ComponentModel.Installation.FileSystem;
using octalforty.Componento.Shared.SemanticModel;
using Rhino.Mocks;

namespace octalforty.Componento.Tests.Componento.ComponentModel.Installation
{
    [TestFixture()]
    public class ComponentDependenciesProviderServiceTestFixture
    {
        [Test()]
        public void GetComponentDependenciesReturnsEmptyArrayWhenNoDependencies()
        {
            MockRepository mockRepository = new MockRepository();
            IFileSystemManagerService fileSystemManager = mockRepository.StrictMock<IFileSystemManagerService>();

            using(mockRepository.Record())
            {
                Expect.Call(fileSystemManager.CurrentDirectory).
                    Return(@"d:\lib");
                Expect.Call(fileSystemManager.FileExists(@"d:\lib\nunit\dependencies.xml")).
                    Return(false);
            } // usong

            ComponentDependenciesProviderService componentDependenciesProvider = 
                new ComponentDependenciesProviderService();
            componentDependenciesProvider.FileSystemManager = fileSystemManager;

            using(mockRepository.Playback())
                Assert.IsEmpty(componentDependenciesProvider.GetComponentDependencies(new ComponentManifest("nunit", new Version(1, 0))));
        }

        [Test()]
        public void GetComponentDependencies()
        {
            MockRepository mockRepository = new MockRepository();
            IFileSystemManagerService fileSystemManager = mockRepository.StrictMock<IFileSystemManagerService>();

            using(mockRepository.Record())
            {
                Expect.Call(fileSystemManager.CurrentDirectory).
                    Return(@"d:\lib");
                Expect.Call(fileSystemManager.FileExists(@"d:\lib\nunit\dependencies.xml")).
                    Return(true);
                Expect.Call(fileSystemManager.OpenFile(@"d:\lib\nunit\dependencies.xml")).
                    Return(new MemoryStream(
                        Encoding.UTF8.GetBytes(
@"<dependencies xmlns=""http://schemas.componento.net/1.0/dependencies.xsd"">
    <dependency name=""nhibernate-linfu"" uri=""file:///f:/linfu"" />
    <dependency name=""nhibernate-castle"" uri=""file:///f:/castle"" version=""1.2.3.4""/>
</dependencies>")));
            } // usong

            ComponentDependenciesProviderService componentDependenciesProvider = 
                new ComponentDependenciesProviderService();
            componentDependenciesProvider.FileSystemManager = fileSystemManager;

            ComponentReference[] dependencies;
         
            using(mockRepository.Playback())
                dependencies = componentDependenciesProvider.GetComponentDependencies(new ComponentManifest("nunit", new Version(1, 0)));

            Assert.IsNotEmpty(dependencies);
            Assert.AreEqual(new ComponentReference("nhibernate-linfu", new Uri("file:///f:/linfu")), dependencies[0]);
            Assert.AreEqual(new ComponentReference("nhibernate-castle", new Version("1.2.3.4"), new Uri("file:///f:/castle")), dependencies[1]);
        }
    }
}
